package ru.reaperoq.taskmanager.controllers

import io.jsonwebtoken.security.SignatureException
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.ControllerAdvice
import org.springframework.web.bind.annotation.ExceptionHandler
import ru.reaperoq.taskmanager.dto.Response
import ru.reaperoq.taskmanager.exceptions.TaskManagerException

@ControllerAdvice
class TaskManagerAdvice {
    @ExceptionHandler(TaskManagerException::class)
    fun handleException(ex: TaskManagerException): ResponseEntity<Response<*>> {
        return ResponseEntity(
            Response.error<Any>(
                message = ex.message,
                code = ex.httpStatus.value()
            ), ex.httpStatus
        )
    }

    @ExceptionHandler(SignatureException::class)
    fun handleSignatureException(ex: SignatureException): ResponseEntity<Response<*>> {
        return ResponseEntity(
            Response.error<Any>(
                message = "Не авторизован",
                code = 401
            ), HttpStatus.UNAUTHORIZED
        )
    }
}